var Utility = {};

/**
 * Display a waiting box while server is processing some export operations
 * and return file within a popup window, work with common browsers (IE, Firefox and Chrome)
 * This is applied for all screens which has export function.
 */
Utility.processExportWaiting = function(url) {
    // Specific for Google Chrome, not display waiting box 
    if (Ext.isChrome) {
        window.location.href = url;
        return;
    }
    var waitingBox = Ext.MessageBox.wait(lang.text.pleaseWait, lang.text.loading);
    if (Ext.isIE7 || Ext.isIE8 || Ext.isChrome) {
        // Reload window with export url
        window.location.href = url;
        
        document.onreadystatechange = function() {
            // Loading status of window switch to interactive when the file is returned completely
            if (document.readyState === "interactive") {
                if (waitingBox != null) {
                    waitingBox.hide();    
                }
            }
        };
    } else { // For Firefox
        // Create an hidden iframe to execute sending request and receiving file
        var downloadIframe = Ext.getBody().createChild({
            tag : 'iframe',
            display : 'none',
            cls : 'download-iframe',
            src : url
        });
        
        // Hide waiting box when iframe is loaded and get the returned file
        downloadIframe.dom.onload = function() {
            waitingBox.hide();
        }
    } 
}

Utility.processExportWaitingWithParams = function(url, params) {
    var url = url + "?" + Ext.urlEncode(params);
    Utility.processExportWaiting(url);
}

Utility.formatThousandSeparator = function(value) {
	var ps = String(value).split('.');
    ps[1] = ps[1] ? ps[1] : null;
    var whole = ps[0];
    var r = /(\d+)(\d{3})/;
    while (r.test(whole)) {
    	whole = whole.replace(r, '$1' + ',' + '$2');
    }
    return whole + (ps[1] ? '.' + ps[1] : '');
}

Utility.autoCompleteDateValue = function(val) {
	return Ext.isDate(val) ? val.format('d-m-Y') : "";
}

Utility.getRawValueOfNumericField = function(field) {
	return parseFloat(field.getValue().replace(/,/gi, ''));
}

Utility.getRawValueOfStringValue = function(value) {
	return parseFloat(value.replace(/,/gi, ''));
}

Utility.createLayoutForComponent = function(item , labelWidth) {
    var container;
    if(labelWidth == null){
        container = new Ext.Container({
            items : [item],
            layout : 'customizedlayout'
        });
    } else {
        container = new Ext.Container({
            items : [item],
            labelWidth : labelWidth,
            layout : 'customizedlayout'
        });
    }
    return container;
}

Utility.lookUp = function(text) {
    //check if there are two arguments in the arguments list
    if (arguments.length <= 1) {
        //if there are not 2 or more arguments there's nothing to replace
        //just return the original text
        return text;
    }
    
    //decrement to move to the second argument in the array
    var tokenCount = arguments.length - 2;
    for ( var token = 0; token <= tokenCount; token++) {
        //iterate through the tokens and replace their placeholders from the original text in order
        text = text.replace(new RegExp("\\{" + token + "\\}", "gi"), arguments[token + 1].toString());
    }
    return text;
}